<template>
	<view class="shouhuoaddress">
		<headertop title="收货地址" str1="oridei" :back1="back1"></headertop>
		<view class="shouhuoaddress_back">

		</view>
		<view class="itemboxss">
			<view class="msgicon" v-if="!list.length">
				<image :src="_static('/images/wxnone.png')" mode="widthFix">
				</image>
				<view class="msgtext">
					暂无地址
				</view>
			</view>
			<view class="itemboxss_itembox" v-for="(item,index) in list" :key="index" @click="xz(item)">
				<view class="center">
					<view class="topbox1">
						<image :src="_static('/images/Frame@2x(22).png')"
							style="z-index: 999;" v-if="item.is_default==1" class="icon" mode=""></image>
						<view class="texts">

							<view class="t1">
								<view style="display: flex;align-items: center;">
									<view class="name">
										{{item.consignee}}
									</view>
									<view class="moblie">
										{{item.mobile}}
									</view>
								</view>
								<image :src="_static('/images/Group 1604@2x.png')"
									v-if="item.is_default==1" class="mobox" mode="">
								</image>
							</view>
							<view class="t2">
								{{item.province_name}}{{item.city_name}}{{item.district_name}}{{item.address}}
							</view>
						</view>
						<image :src="_static('/images/Frame@2x(23).png')"
							@click.stop="bianji(item)" class="bianjiicon" mode="">
						</image>
					</view>
					<view class="bottombox">
						<view class="xzbox" @click.stop="mr(item)">
							<image
								:src="item.is_default==1?_static('/images/Group 11730@2x.png'):_static('/images/Ellipse 31@2x.png')"
								mode="">
							</image>
							<span>默认地址</span>
						</view>
						<span @click.stop="remove1(item)">删除</span>
					</view>
				</view>
			</view>

			<view class="" style="height: 210rpx;">

			</view>
		</view>
		<view class="fixedbox" @click="gopage()">
			<view class="centerbox">
				<span style="font-size:40rpx ;">+ </span> 添加新地址
			</view>
		</view>
	</view>

</template>

<script setup>
	import {
		_static
	} from '@/ulm/utils/url.js'
	import headertop from '@/components/header.vue'
	import {
		onPageScroll,
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import ulm, {
		$api
	} from '@/ulm';
	import {
		ref
	} from 'vue'
	const back1 = ref('')
	const list = ref([])
	const typestr = ref('')
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#81D6FF'
		} else {
			back1.value = ''
		}
	})
	onLoad((opt) => {
		typestr.value = opt.typestr
	})
	onShow(() => {
		init()
	})
	async function init() {
		const res = await $api.addressIndex()
		if (res.code == 1) {
			list.value = res.data
		}
	}

	function bianji(item) {
		uni.navigateTo({
			url: '/sub_my/my/bjshdz?id=' + item.id
		})
	}

	function gopage() {
		uni.navigateTo({
			url: '/sub_my/my/bjshdz'
		})
	}

	async function xz(item) {
		if (typestr.value != 'dz') {
			uni.navigateTo({
				url: '/sub_my/my/bjshdz?id=' + item.id
			})
			return
		}

		const res = await $api.editAddress({
			id: item.id,
			is_default: 1,
			consignee: item['consignee'],
			mobile: item['mobile'],
			province_name: item['province_name'],
			city_name: item['city_name'],
			district_name: item['district_name'],
			address: item['address'],
		})
		if (res.code == 1) {
			uni.navigateBack()
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}
	async function mr(item) {
		if (item.is_default == 1) return
		const res = await $api.editAddress({
			id: item.id,
			is_default: 1,
			consignee: item['consignee'],
			mobile: item['mobile'],
			province_name: item['province_name'],
			city_name: item['city_name'],
			district_name: item['district_name'],
			address: item['address'],
		})
		if (res.code == 1) {
			setTimeout(() => {
				uni.showToast({
					title: '已设为默认地址',
					icon: 'none'
				})
			}, 500)
			init()
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}

	function remove1(item) {
		uni.showModal({
			title: '提示', // 标题（可选）
			content: '确定要删除吗？', // 内容（可选）
			confirmText: '确定', // 确认按钮文字（可选，默认“确定”）
			cancelText: '取消', // 取消按钮文字（可选，默认“取消”）
			success: (res) => {
				if (res.confirm) {
					remove(item)
					// 执行确定后的逻辑
				} else if (res.cancel) {

					// 执行取消后的逻辑
				}
			},
			fail: (err) => {

			}
		});
	}
	async function remove(item) {
		const res = await $api.deleteaddress({
			id: item.id
		})
		if (res.code == 1) {
			init()
			setTimeout(() => {
				uni.showToast({
					title: '删除成功',
					icon: 'none'
				}, 500)
			})
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}
</script>
<style lang="scss">
	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: 30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	page {
		background: #F7F8FA;
	}

	.shouhuoaddress {
		width: 100%;
		overflow: hidden;
		position: relative;

		.fixedbox {
			width: 100%;
			height: 210rpx;
			background: #FFFFFF;
			box-shadow: 0rpx -4rpx 8rpx 0rpx rgba(0, 0, 0, 0.04);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			overflow: hidden;
			position: fixed;
			bottom: 0%;

			.centerbox {
				width: 686rpx;
				height: 88rpx;
				background: #2AB4F5;
				border-radius: 200rpx 200rpx 200rpx 200rpx;
				text-align: center;
				line-height: 88rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 30rpx;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 30rpx auto;
			}
		}

		&_back {
			height: 314rpx;
			background: linear-gradient(180deg, #CAEEFF 71%, rgba(240, 250, 255, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			width: 100%;
			position: absolute;
			z-index: -1;
		}

		.itemboxss {
			width: 686rpx;
			margin: auto;
			overflow: hidden;
			margin-top: 204rpx;

			&_itembox {
				width: 686rpx;
				background: #FFFFFF;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				overflow: hidden;
				margin-bottom: 32rpx;

				.center {
					width: 636rpx;
					margin: auto;

					.bottombox {
						width: 100%;
						height: 88rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #777777;
						margin-top: 32rpx;
						border-top: 1rpx solid #F2F2F2;

						.xzbox {
							display: flex;
							align-items: center;

							image {
								width: 32rpx;
								height: 32rpx;
								margin-right: 24rpx;
							}
						}
					}

					.topbox1 {
						width: 100%;
						display: flex;
						align-items: center;
						margin-top: 24rpx;
						justify-content: space-between;

						.icon {
							width: 48rpx;
							height: 48rpx;
						}

						.bianjiicon {
							width: 32rpx;
							height: 32rpx;
							margin-left: 88rpx;
						}

						.texts {
							width: 446rpx;
							margin-left: 26rpx;

							.t1 {
								width: 100%;
								display: flex;
								align-items: center;
								justify-content: space-between;

								.name {
									font-family: PingFang SC, PingFang SC;
									font-weight: 500;
									font-size: 32rpx;
									color: #333333;
									max-width: 150rpx;
									line-height: 32rpx;
									overflow: hidden;
									white-space: nowrap;
									text-overflow: ellipsis;
								}

								.moblie {
									font-family: PingFang SC, PingFang SC;
									font-weight: 400;
									font-size: 32rpx;
									line-height: 32rpx;
									color: #333333;
									margin-left: 16rpx;
								}

								.mobox {
									width: 80rpx;
									height: 34rpx;
								}
							}

							.t2 {
								width: 100%;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #777777;
								margin-top: 24rpx;
								display: -webkit-box;
								/* 必须设置，用于多行溢出 */
								-webkit-box-orient: vertical;
								/* 设置盒子排列方向为垂直 */
								-webkit-line-clamp: 2;
								/* 限制显示的行数，例如显示 3 行 */
								overflow: hidden;
								/* 隐藏超出的内容 */
							}
						}
					}
				}
			}
		}
	}
</style>